<?php
namespace haibao\cms\business\control\wap\common;

use haibao\cms\business\control\PublicController;
class FeatureController extends \haibao\cms\business\control\Base{
	
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$path = $this->generateHtmlPath(__CLASS__);
		$result = array();
		if($controlData['manual']){
			$dataCondition = $controlData['manual'];
			$currentPage = $this->getParameter(self::$CURRENTPAGE);
			$result = \haibao\cms\business\control\FeatureController::getMobileFeatureList($currentPage,$dataCondition['limit']);
			foreach ($result as $list){
				$list->PublishTime = $this->formatDate($list->ListTime->format('Y-m-d H:i:s'));
				$list->Image->Filename = $this->replaceImageUrl($list->Image->Filename);
			}
		}
		$this->assign('result', $result);
		$this->assign('currentPage',$this->getParameter(self::$CURRENTPAGE));
		$this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	public function replaceImageUrl($imgUrl){
		$staticImageUrl = $this->replaceImageLink($imgUrl,750,435);
		$key = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_IMG_ENCRYPT_KEY);
		if(\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_CDN_DEFAULT)){
			$newImgName = md5($staticImageUrl.$key);
			$hasCount = strpos($staticImageUrl, \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_STATIC_DOMAIN).'/img');
				
			$cdnNum = $this->getHash($newImgName);
			$imgUrl = \haibao\cms\Config::$cdnNewHost[$cdnNum];
			$mtUrl = str_replace(\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_STATIC_DOMAIN).'/img',$imgUrl,$staticImageUrl);
			return $mtUrl;
				
		}else{
			return $staticImageUrl;
		}
	}
	
	private static function getHash($key,$isNew = true){
		$hash = crc32($key) >> 16 & 0xffff;
		$cdnHostArr = \haibao\cms\Config::$cdnNewHost;
		if(!$isNew){
			$cdnHostArr = \haibao\cms\Config::$cdnHost;
		}
		return sprintf('%01s',$hash % count($cdnHostArr));
	}
}